package com.itheima.consumer.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 延时消息
 *
 * @author chenzhihua
 * @Date 2024/5/16
 */
@Configuration
public class DelayExchangeConfig {
    /**
     * 声明一个延迟交换机
     *
     * @return
     */
    @Bean
    public DirectExchange delayExchange() {
        return ExchangeBuilder
                .directExchange("delay.direct")  // 指定交换机名称
                .delayed() // 开启延时
                .durable(true) // 开启持久化
                .build();
    }

    /**
     * 声明一个延迟队列
     */
    @Bean
    public Queue delayQueue() {
        return new Queue("delay.queue");
    }

    /**
     * 声明一个绑定
     */
    @Bean
    public Binding delayBinding() {
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with("delay");
    }
}
